﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Cookies
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["userInfo"] != null)
            {
                string userName = Server.HtmlEncode(Request.Cookies["userInfo"]["uiUserName"]);
                header.InnerText = "Hej " + userName;

                HttpCookie appCookie = Request.Cookies["userInfo"];

                foreach (Control c in form1.Controls)
                {
                    if (c is TextBox)
                    {
                        TextBox txt = (TextBox)c;
                        string s = Request.Cookies["userInfo"][txt.ID];
                        txt.Text = s;
                    }
                    /*if (c.GetType() == typeof(TextBox))
                    {
                    }*/
                }
            }

            if (Page.IsPostBack)
            {
                if (Request.Cookies["userinfo"] == null)
                {
                    HttpCookie appCookie = new HttpCookie("userInfo");
                    
                    foreach (Control c in form1.Controls)
                    {
                        if (c is TextBox)
                        {
                            TextBox txt = (TextBox)c;
                            appCookie.Values.Add(txt.ID, txt.Text);
                        }
                    }

                    appCookie.Expires = DateTime.Now.AddMinutes(3);
                    Response.Cookies.Add(appCookie);
                    header.InnerText = "Hej " + uiUserName.Text;
                }
            }
        }
    }
}